/**
 * 企业客户管理初始化
 */
var CustomerCompany = {
    id: "CustomerCompanyTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1

};

/**
 * 初始化表格的列
 */
CustomerCompany.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '企业编号', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '权限', field: 'authority', visible: false, align: 'center', valign: 'middle'},
        {title: '企业名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '统一社会信用代码', field: 'unifiedSocialCreditCode', visible: true, align: 'center', valign: 'middle'},
        {title: '企业简称', field: 'shortName', visible: true, align: 'center', valign: 'middle'},
        {title: '法定代表人', field: 'legalPerson', visible: true, align: 'center', valign: 'middle'},
        {title: '所属集团', field: 'corporation', visible: true, align: 'center', valign: 'middle'},
        {title: '所属板块', field: 'section', visible: true, align: 'center', valign: 'middle'},
        {title: '军工代码', field: 'militaryCode', visible: true, align: 'center', valign: 'middle'},
        {title: '所属区域', field: 'areaName', visible: true, align: 'center', valign: 'middle'},
        {title: '主营业务或产品', field: 'mainBusiness', visible: true, align: 'center', valign: 'middle'},
        {
            title: '操作',
            field: "",
            visible: true,
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<button class="btn btn-info btn-sm rightSize detailBtn" type="button" onclick="CustomerCompany.showRelation(' + row.id + ')"><i class=""></i>更多</button>';
            }
        }
    ];
};


CustomerCompany.detail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '客户详情',
            area: ['1250px', '700px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/customerCompany/customerCompany_detail/' + CustomerCompany.seItem.id
        });
        layer.full(index);
        this.layerIndex = index;
    }
}



CustomerCompany.showRelation = function (id) {
    var index = layer.open({
        type: 2,
        title: '关联客户',
        area: ['450px', '500px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customerRelation/customer_relation_detail/' + id
    });
    this.layerIndex = index;
}


CustomerCompany.visitDetail = function () {
    var index = layer.open({
        type: 2,
        title: '拜访详情',
        area: ['1250px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/visit/visit_detail/' + $("#comId").val()
    });
    this.layerIndex = index;

}

CustomerCompany.payPlanDetail = function () {
    var index = layer.open({
        type: 2,
        title: '付款详情',
        area: ['1250px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/payPlan/payPlan_detail/' + $("#comId").val()
    });
    this.layerIndex = index;

}

CustomerCompany.purchaseDetail = function () {
    var index = layer.open({
        type: 2,
        title: '采购详情',
        area: ['1250px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/purchase/purchase_detail/' + $("#comId").val()
    });
    this.layerIndex = index;

}

CustomerCompany.saleDetail = function () {
    var index = layer.open({
        type: 2,
        title: '销售详情',
        area: ['1250px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/sale/sale_detail/' + $("#comId").val()
    });
    this.layerIndex = index;

}

CustomerCompany.Detail = function () {
    var index = layer.open({
        type: 2,
        title: '企业管理详情',
        area: ['1250px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customerCompanyManage/customerCompanyManage_detail/' + $("#comId").val()
    });
    this.layerIndex = index;

}

CustomerCompany.contactDetail = function () {
        var index = layer.open({
            type: 2,
            title: '联系人详情',
            area: ['1250px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/contact/contact_detail/' + $("#comId").val()
        });
        this.layerIndex = index;

}


/**
 * 检查是否选中
 */
CustomerCompany.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        CustomerCompany.seItem = selected[0];
        return true;
    }
};

CustomerCompany.checkPermission = function(){
    if (this.check()) {
        if (CustomerCompany.seItem.authority === 1) {
            return true
        }else{
            Feng.info("您没有该操作权限！");
        }
    }
};

/**
 * 点击添加企业客户
 */
CustomerCompany.openAddCustomerCompany = function () {
    var index = layer.open({
        type: 2,
        title: '添加企业客户',
        area: ['1250px', '700px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customerCompany/customerCompany_add'
    });
    layer.full(index);
    this.layerIndex = index;
};

/**
 * 打开查看企业客户详情
 */
CustomerCompany.openCustomerCompanyDetail = function () {
    if (this.checkPermission()) {
        var index = layer.open({
            type: 2,
            title: '企业客户详情',
            area: ['350px', '200px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/customerCompany/customerCompany_update/' + CustomerCompany.seItem.id
        });
        this.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 删除企业客户
 */
CustomerCompany.delete = function () {
    if (this.check()) {
        var operation = function () {
            var customerCompanyId = CustomerCompany.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/customerCompany/delete", function (data) {
                Feng.success("删除成功!");
                CustomerCompany.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("customerCompanyId", CustomerCompany.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否删除 " + CustomerCompany.seItem.name + "?", operation);
    }
};


CustomerCompany.openRelation = function () {
    if (this.checkPermission()) {
        var index = layer.open({
            type: 2,
            title: '关联企业',
            area: ['300px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/customerRelation/customer_relation/' + this.seItem.id
        });
        this.layerIndex = index;
    }
}

CustomerCompany.resetSearch = function () {
    $("#name").val("");
    CustomerCompany.search();
}
/**
 * 查询企业客户列表
 */
CustomerCompany.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    CustomerCompany.table.refresh({query: queryData});
};


CustomerCompany.exportXlsByT = function () {
    window.location.href = Feng.ctxPath + "/customerCompany/exportXlsByM";
}

$(function () {
    var defaultColunms = CustomerCompany.initColumn();
    var table = new BSTable(CustomerCompany.id, "/customerCompany/list", defaultColunms);
    table.setPaginationType("server");
    CustomerCompany.table = table.init();

    var upload = new $WebUpload({
        uploadId: "upload",
        mimeTypes: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
        extensions: "xlsx,xls",
        url: "/customerCompany/importExcel",
        multiple: false
    });
    upload.init();
});
